﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectPhanHoi()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_PHANHOI
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectPhanHoiPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_PHANHOI,
				CurrentPage = page
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectPhanHoiByHopThu(int idhopthu, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDHopThu = idhopthu,
				EnumViewModelType = EnumViewModel.ADMIN_PHANHOI,
				CurrentPage = page
			};
			return View("SelectPhanHoi", CreateViewModel(data));
		}

		public ActionResult SelectPhanHoiByNguoiPhanHoi(Guid idnguoiphanhoi, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDNguoiDang = idnguoiphanhoi,
				EnumViewModelType = EnumViewModel.ADMIN_PHANHOI,
				CurrentPage = page
			};
			return View("SelectPhanHoi", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeletePhanHoi(int id)
		{
			return Json(new{
				Success = _repPhanHoi.Delete(id),
				RecordCount = _repPhanHoi.SelectAll().Count,
				DeleteId = id
			});
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertPhanHoi(FormCollection forms)
		{
			string idhopthu = forms.GetValues("PhanHoi_IDHopThu")[0];
			string idnguoiphanhoi = forms.GetValues("PhanHoi_IDNguoiPhanHoi")[0];
			string noidungphanhoi = forms.GetValues("PhanHoi_NoiDungPhanHoi")[0];
            string noidungphanhoirutgon = forms.GetValues("PhanHoi_NoiDungPhanHoiRutGon")[0];
			string ngaydang = forms.GetValues("PhanHoi_NgayDang")[0];
            string to = forms.GetValues("PhanHoi_Email")[0];
            string tieude = forms.GetValues("PhanHoi_TieuDe")[0];
            string noidung = forms.GetValues("PhanHoi_NoiDung")[0];

            var emailContent = RenderPartialViewToString("PhanHoiEmailTemplate");
            emailContent = emailContent.Replace("[TieuDe]", tieude);
            emailContent = emailContent.Replace("[NoiDung]", noidung);
            emailContent = emailContent.Replace("[NoiDungPhanHoi]", noidungphanhoi);

            var result = SendingMail("Thư phản hồi từ - " + WebConfiguration.SiteName, emailContent, to);
            if (result)
            {
                var newItem = new PhanHoi
                {
                    IDHopThu = int.Parse(idhopthu),
                    IDNguoiPhanHoi = Guid.Parse(idnguoiphanhoi),
                    NoiDungPhanHoi = noidungphanhoi,
                    NoiDungPhanHoiRutGon = noidungphanhoirutgon,
                    NgayDang = DateTime.Parse(ngaydang)
                };
                result = _repPhanHoi.Insert(newItem);
            }
			var data = new DataTransferViewModel
			{
                IDHopThu = int.Parse(idhopthu),
				EnumViewModelType = EnumViewModel.ADMIN_PHANHOI,
				Added = result
			};
			return View("SelectPhanHoi", CreateViewModel(data));
		}

		public ActionResult DetailOfPhanHoi(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_PHANHOI,
				EditID = id
			};
			return View(CreateViewModel(data));
		}

	}
}